﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XRepair.Models;
using RepositoryUtil.SQL;
using XRepair.Conventions.Enum;

namespace MvcXRepairCustomer.Repository.SQL
{
    public class StateDAO : BaseDAO
    {
        public IEnumerable<State> GetAllActiveStates()
        {
            string spName = "GetAllActiveStates";

            DataTable resultTab = null;
            List<State> stateList = null;

            using (var conn = XRepairConn)
            {
                conn.Open();

                SqlCommand command = new SqlCommand(spName, conn);
                command.CommandType = CommandType.StoredProcedure;
                resultTab = command.ExecuteDataTable();
            }
            if (resultTab != null && resultTab.Rows.Count > 0)
            {
                stateList = this.ConvertDataTableToStateList(resultTab);
            }

            return stateList;

        }

        public bool DeactivateStateByID(int stateId)
        {
            string spName = "DeactivateStateByID";

            object Rowcount = null;

            using (var conn = XRepairConn)
            {
                conn.Open();

                SqlCommand command = new SqlCommand(spName, conn);
                command.CommandType = CommandType.StoredProcedure;

                command.AddInParameter("ID", stateId);
                command.AddOutParameter("Rowcount", -1);
                command.ExecuteNonQuery();

                Rowcount = command.GetParameterValue("Rowcount");
            }

            long RowcountValue;

            if (long.TryParse(Rowcount.ToString(), out RowcountValue) && RowcountValue != -1)
            {
                return true;
            }

            return false;
        }

        public bool UpdateState(State state)
        {
            string spName = "UpdateState";
            object Rowcount = null;

            using (var conn = XRepairConn)
            {
                conn.Open();

                SqlCommand command = new SqlCommand(spName, conn);
                command.CommandType = CommandType.StoredProcedure;

                command.AddInParameter("ID", state.ID);
                command.AddInParameter("StateAbbreviation", state.StateAbbreviation);
                command.AddInParameter("StateName", state.StateName);
                command.AddInParameter("LastUpdatedAt", state.LastUpdatedAt);
                command.AddInParameter("LastUpdatedBy", state.LastUpdatedBy);

                command.AddOutParameter("Rowcount", -1);
                command.ExecuteNonQuery();

                Rowcount = command.GetParameterValue("Rowcount");
            }

            long RowcountValue;

            if (long.TryParse(Rowcount.ToString(), out RowcountValue) && RowcountValue != -1)
            {
                return true;
            }

            return false;
        }

        public bool AddNewState(State state)
        {
            string spName = "AddNewState";
            object newIdentity = null;

            using (var conn = XRepairConn)
            {
                conn.Open();

                SqlCommand command = new SqlCommand(spName, conn);
                command.CommandType = CommandType.StoredProcedure;

                command.AddInParameter("StateAbbreviation", state.StateAbbreviation);
                command.AddInParameter("StateName", state.StateName);
                command.AddInParameter("CreatedAt", state.CreatedAt);
                command.AddInParameter("CreatedBy", state.CreatedBy);

                command.AddOutParameter("NewIdentity", -1);
                command.ExecuteNonQuery();

                newIdentity = command.GetParameterValue("NewIdentity");
            }

            long newIdentityValue;

            if (long.TryParse(newIdentity.ToString(), out newIdentityValue) && newIdentityValue != -1)
            {
                return true;
            }

            return false;
        }

        private List<State> ConvertDataTableToStateList(DataTable data)
        {
            List<State> states = null;

            if (data != null)
            {
                states = new List<State>();

                foreach (DataRow dr in data.Rows)
                {
                    var serviceModel = new State();

                    serviceModel.ID = SqlUtil<int>.GetValue(dr, "ID");
                    serviceModel.StateAbbreviation = SqlUtil<string>.GetValue(dr, "StateAbbreviation");
                    serviceModel.StateName = SqlUtil<string>.GetValue(dr, "StateName");
                    serviceModel.IsActive = SqlUtil<bool>.GetValue(dr, "IsActive");
                    serviceModel.CreatedAt = SqlUtil<DateTime>.GetValue(dr, "CreatedAt");
                    serviceModel.CreatedBy = SqlUtil<String>.GetValue(dr, "CreatedBy");
                    serviceModel.LastUpdatedAt = SqlUtil<DateTime>.GetValue(dr, "LastUpdatedAt");
                    serviceModel.LastUpdatedBy = SqlUtil<string>.GetValue(dr, "LastUpdatedBy");

                    states.Add(serviceModel);
                }
            }

            return states;
        }

    }
}
